//引入时间处理函数
var util = require('../../../utils/util.js');
var network = require("../../../utils/network.js")
var flag = false
var arrayId=[]
var defaultDay=""
var timechecked1=""
var timechecked2 = ""
var timechecked3 = ""
var timechecked4 = ""
var timechecked5 = ""
var timechecked6 = ""
var timechecked7 = ""
//获取应用实例
var app = getApp()
Page({
  data: {
    getFormatTime: "",
    ischecked: 1,
    ischeckeday: [],   
    timeCheck: 1,
    hourId: 0,//默认第一天被选中
  },
  onLoad: function (options) {
    var that = this 
    // 请求日期列表
    var data = {}
    data.openid = wx.getStorageSync("openid")
    data.uid = wx.getStorageSync("uid")
    data.token = wx.getStorageSync("token")
    network.getData(
      "https://api.hsb1234.com/userc/v1/baojie/days",
      data,
      function(res){
        var defaultDaysList = res.data.data.list
        var hourId=""
        console.log(defaultDaysList)
        for (var i = 0; i < defaultDaysList.length;i++){
          defaultDaysList[i].id = i 
          if (defaultDaysList[i].isValid){
            defaultDaysList[i].disabled =""
            defaultDaysList[i].color = ""
            defaultDaysList[i].hold = ""          
          }else{
            defaultDaysList[i].disabled = "disabled"
            defaultDaysList[i].color = "ddd"
            defaultDaysList[i].hold = "约满"     
          }
        }
        // 遍历默认加载没有约满的日期序列
        for (var i = 0; i < defaultDaysList.length; i++) {
          if (defaultDaysList[i].isValid) {
            hourId = i 
            break
          } 
        } 
        var data2={}
        var userid = wx.getStorageSync("userid")
        if (userid) { return } else { userid = "" }
        data2.day = defaultDaysList[hourId].day
        data2.duration = wx.getStorageSync("duration")
        data2.userid = userid
        data2.openid = wx.getStorageSync("openid")
        data2.uid = wx.getStorageSync("uid")
        data2.token = wx.getStorageSync("token")
        // 加载默认的时间列表
        network.getData(
          "https://api.hsb1234.com/userc/v1/baojie/timelist",
          data2,
          function (res) {
            console.log(res)
            var resdata = res.data.data.list
            for (var i = 0; i < resdata.length; i++) {
              resdata[i].id = i
              if (resdata[i].isValid) {
                resdata[i].disabled = ""
                resdata[i].color = ""
                resdata[i].hold = ""
              } else {
                resdata[i].disabled = "disabled"
                resdata[i].color = "ddd"
                resdata[i].hold = "(约满)"
              }
            }
            that.setData({
              timeList: resdata
            })
          },
          function(){}
        )
        console.log(defaultDaysList)
        console.log(hourId)
        that.setData({
          daysList: defaultDaysList,
          hourId: hourId,
          daychecked: hourId
        })
      },
      function(){
      }
    )
  },
  daysChange: function (e) {
    var that = this 
    var index = e.detail.value
    var daysList = this.data.daysList
    var userid = wx.getStorageSync("userid")
    if (userid) { return } else { userid=""}
    var data={}
    data.day = daysList[index].day
    data.duration = wx.getStorageSync("duration")
    data.userid = userid
    data.openid = wx.getStorageSync("openid")
    data.uid = wx.getStorageSync("uid")
    data.token = wx.getStorageSync("token")
    wx.request({
      url: "https://api.hsb1234.com/userc/v1/baojie/timelist",
      data:data,
      header: {
        'content-Type': 'application/json'
      },
      method: "GET",
      success:function(res){
        console.log(res)
        var resdata = res.data.data.list
        for (var i = 0; i < resdata.length; i++) {
          resdata[i].id = i
          if (resdata[i].isValid) {
            resdata[i].disabled = ""
            resdata[i].color = ""
            resdata[i].hold = ""
          } else {
            resdata[i].disabled = "disabled"
            resdata[i].color = "ddd"
            resdata[i].hold = "(约满)"
          }
        }
        that.setData({
          timeList: resdata
        })
      },
      fail: function (res) {
        if (res.statusCode == 401) {
          wx.navigateTo({
            url: '../../public/login/index',
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '加载失败请检查您的网络',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      },
    })
    this.setData({
      daychecked: index,
      hourId: index
    })
  },
  timesChange1: function (e) {
    var that = this
    var index = e.detail.value
    that.setData({
      ischeckeTime1: e.detail.value
    })
    timechecked1 = that.data.timeList[index]
   
  },
  timesChange2: function (e) {
    var that = this
    var index = e.detail.value 
    that.setData({
      ischeckeTime2: e.detail.value
    })
    timechecked2 = that.data.timeList[index]
   
  },
  timesChange3: function (e) {
    var that = this
    var index = e.detail.value 
    that.setData({
      ischeckeTime3: e.detail.value
    })
    timechecked3 = that.data.timeList[index]
  },
  timesChange4: function (e) {
    var that = this
    var index = e.detail.value 
    that.setData({
      ischeckeTime4: e.detail.value
    })
    timechecked4 = that.data.timeList[index]
   
  },
  timesChange5: function (e) {
    var that = this
    var index = e.detail.value 
    that.setData({
      ischeckeTime5: e.detail.value
    })
    timechecked5 = that.data.timeList[index]
  
  },
  timesChange6: function (e) {
    var that = this
    var index = e.detail.value 
    that.setData({
      ischeckeTime6: e.detail.value
    })
    timechecked6 = that.data.timeList[index]
   
  },
  timesChange7: function (e) {
    var that = this
    var index = e.detail.value 
    that.setData({
      ischeckeTime7: e.detail.value
    })
    timechecked7 = that.data.timeList[index]
   
  },
  upTime:function(e){
    var ischeckTimeList = []
    ischeckTimeList.push(timechecked1)
    ischeckTimeList.push(timechecked2)
    ischeckTimeList.push(timechecked3)
    ischeckTimeList.push(timechecked4)
    ischeckTimeList.push(timechecked5)
    ischeckTimeList.push(timechecked6)
    ischeckTimeList.push(timechecked7)
    console.log(ischeckTimeList)
    var daychecked = this.data.daychecked
    if (!ischeckTimeList[daychecked].time){
      wx.showModal({
        title: '提交失败',
        content: '请你选择服务时间',
      })
    }else{
      var daysList = this.data.daysList 
      var newcheckTimeList={}
      newcheckTimeList.appointDay = daysList[daychecked].day
      newcheckTimeList.appointweek = daysList[daychecked].title
      newcheckTimeList.appointTime = ischeckTimeList[daychecked].time
      console.log(newcheckTimeList)
      // wx.setStorageSync("newcheckTimeList", newcheckTimeList)
      wx.setStorage({
        key: "newcheckTimeList",
        data: newcheckTimeList,
        success:function(){
          wx.navigateBack({
            url: '../../homeClean/hc-appointment/index',
          })
        }
      })
      
    }
  }
})
